<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" slot="detail" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="行内编码" prop="innercode">
              <!-- <a-input v-model="model.innercode" placeholder="请输入行内编码"></a-input> -->
              <a-select v-model="model.innercode" style="width: 100%" allowClear placeholder="请选择行内编码">
                <a-select-option v-for="el in innercodeList" :key="el.value" :value="el.value">
                  {{ el.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="金融机构编码" prop="bankcode">
              <a-input v-model="model.bankcode" placeholder="请输入金融机构编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="金融机构名称" prop="financename">
              <a-input v-model="model.financename" placeholder="请输入金融机构名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="法人名称" prop="bankname">
              <a-input v-model="model.bankname" placeholder="请输入法人名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="法人标识" prop="bankNoCd">
              <a-input v-model="model.bankNoCd" :disabled="flag" placeholder="请输入法人标识"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="营业执照所载的成立日期"
              prop="busLicenseEstablishDate"
            >
              <j-date
                v-model="model.busLicenseEstablishDate"
                placeholder="请选择营业执照所载的成立日期"
                style="width: 100%"
              ></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="注册资金" prop="registeredCapital">
              <a-input-number
                v-model="model.registeredCapital"
                placeholder="请输入注册资金"
                style="width: 100%"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="注册地址" prop="registeredAddress">
              <a-input v-model="model.registeredAddress" placeholder="请输入注册地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="经营管理总部地址"
              prop="headOfficeAddress"
            >
              <a-input v-model="model.headOfficeAddress" placeholder="请输入经营管理总部地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="币种" prop="currency">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择币种"
                v-model="model.currency"
                dict="jrsj_bz_config,name,code"
                pidValue="1382973175608823809"
                hasChildField="has_child"
              >
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <!--
          <a-col :span="24">
            <a-form-model-item v-show="!reportDisabled" :labelCol="labelCol" :wrapperCol="wrapperCol" label="报表">
              <j-multi-select-tag
                v-model="model.selectedreports"
                :disabled="disableSubmit"
                :options="reportsOptions"
                placeholder="请选择报表">
              </j-multi-select-tag>
            </a-form-model-item>
          </a-col>
          -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction } from '@/api/manage'

import { queryall2, queryBankReport2, getPraentDepartInfo } from '@/api/api'

export default {
  name: 'LlbbJuridicalPersonForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      flag: false,
      reportDisabled: false, //是否是报表维护调用该页面
      disableSubmit: false,
      reportsOptions: [],
      bankid: '', //保存法人id
      // nextDepartOptions:[],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        innercode: [{ required: true, message: '请输入行内编码!' }],
        bankcode: [
          { required: true, message: '请输入金融机构编码!' },
          {
            type: 'string',
            max: 14,
            message: '输入有误，金融机构编码长度需为14位',
            trigger: 'change',
          },
          {
            type: 'string',
            pattern: /[0-9A-Z]{14}/,
            message: '请输入正确的金融机构编码',
            trigger: 'blur',
          },
        ],
        financename: [{ required: true, message: '请输入金融机构名称!' }],
        bankname: [{ required: true, message: '请输入法人名称!' }],
        bankNoCd: [{ required: true, message: '请输入法人标识!' }],
      },
      url: {
        add: '/juridicalPersonManage/llbbJuridicalPerson/add',
        edit: '/juridicalPersonManage/llbbJuridicalPerson/edit',
        queryById: '/juridicalPersonManage/llbbJuridicalPerson/queryById',
      },
      innercodeList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initReportList2()
    this.initReportList2() // 解决第一次无法加载数据问题
    getPraentDepartInfo().then((res) => {
      if (res.success) {
        this.innercodeList = res.result.map((item, index, arr) => {
          let c = { label: item.DEPARTNAME, value: item.ORGCODE }
          return c
        })
      }
    })
  },
  methods: {
    add() {
      this.refresh()
      this.edit(this.modelDefault)
    },
    refresh() {
      this.bankid = ''
    },
    edit(record) {
      if (record.bankNoCd != '' && record.bankNoCd != undefined) {
        this.flag = true
      }
      this.visible = true
      this.bankid = record.bankNoCd
      this.model = Object.assign({}, { selectedreports: '' }, record)
      if (record.hasOwnProperty('bankNoCd')) {
        this.getBankReports2(record.bankNoCd)
      }
      console.log('this.model=', this.model)
    },
    //初始化报表表记录
    initReportList2() {
      queryall2().then((res) => {
        if (res.success) {
          this.reportsOptions = res.result.map((item, index, arr) => {
            let c = { label: item.reporttypename, value: item.id }
            return c
          })
          console.log('this.reportsOptions: ', this.reportsOptions)
        }
      })
    },
    // 获取法人的报表
    getBankReports2(bankid) {
      queryBankReport2({ bankid: bankid }).then((res) => {
        if (res.success) {
          this.model.selectedreports = res.result.join(',')
          console.log('this.model.selectedreports=', this.model.selectedreports)
        }
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id || !that.flag) {
            // id可编辑即新增
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }

          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>